package com.software.mty.shenhao;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import common.APIService;
import common.IHttpCallback;
import common.OkHttpHelper;
import model.TemperatureModel;

public class Temperature extends AppCompatActivity implements IHttpCallback {

    ListView listView;
    List<TemperatureModel> mDatas;

    Handler handler;


    @SuppressLint("HandlerLeak")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_temperature);


        listView = findViewById(R.id.listView);

        mDatas = new ArrayList<>();

        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                Bundle bundle = msg.getData();
                String result = bundle.getString("result");
                String error = bundle.getString("error");
                if (error != null) {
                    Toast.makeText(getApplicationContext(), "获取数据出错！" + error, Toast.LENGTH_SHORT).show();
                    TextView tvMsg = findViewById(R.id.tip_msg);
                    tvMsg.setText("获取数据出错！" + error);
                    tvMsg.setVisibility(View.VISIBLE);
                } else {
                    try {
                        callback(result);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

            }
        };


        OkHttpHelper helper = new OkHttpHelper();
        helper.OkGet(APIService.getTemperature(), handler);


    }


    void bindData(List<TemperatureModel> list) {

        listView.setAdapter(new CommonAdapter<TemperatureModel>(this, R.layout.item_temperate, list) {
            @Override
            protected void convert(ViewHolder viewHolder, TemperatureModel item, int position) {
                viewHolder.setText(R.id.tv_temperature, item.getValue() + "   ℃");
                viewHolder.setText(R.id.tv_time, "时间：" + item.getDateTime());
            }
        });

    }


    @Override
    public void callback(String result) throws JSONException {
        JSONObject jsonObject = new JSONObject(result);
        JSONArray jsonArray = jsonObject.getJSONArray("data");
        for (int i = 0; i < jsonArray.length(); i++) {
            TemperatureModel model = new TemperatureModel();
            model.setDateTime(jsonArray.getJSONObject(i).getString("dateTime"));
            model.setGuid(jsonArray.getJSONObject(i).getString("guid"));
            model.setValue(jsonArray.getJSONObject(i).getLong("value"));
            mDatas.add(model);
        }
        bindData(mDatas);
        Log.e("tag", result);
    }


}
